package com.servlet;

import com.pojo.Bank;
import com.util.BaseServlet;
import com.util.ResultData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import static com.util.Vessel.bankSer;

@WebServlet("/bank")
public class BankServlet extends BaseServlet {
    @Override
    public Class getServlet() {
        return BankServlet.class;
    }

    //全查询
    public String queryBank(HttpServletRequest request, HttpServletResponse response){
        List<Bank> bankList = bankSer.queryBank();
        HttpSession session = request.getSession();
        session.setAttribute("bankList",bankList);
        return "bankList";
    }

    //根据id详细查询
    public String idQueryBank(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        Bank bank = new Bank();
        bank.setId(id);
        List<Bank> bankList1 = bankSer.idQueryBank(bank);
        HttpSession session = request.getSession();
        session.setAttribute("bankList1",bankList1);
        return "bankDetails";
    }

    //判断营业号
    public ResultData noQueryBank(HttpServletRequest request, HttpServletResponse response){
        String businessNo = request.getParameter("businessNo");
        Bank bank = new Bank();
        bank.setBusinessNo(businessNo);
        int i = bankSer.noQueryBank(bank);
        ResultData resultData = new ResultData();
        if (i>0){
            resultData.setMsg("");
        }else {
            resultData.setMsg("营业执照号已经存在");
        }
        return resultData;
    }

    //添加
    public void addBank(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String businessNo = request.getParameter("businessNo");
        String cityArea = request.getParameter("cityArea");
        String address = request.getParameter("address");
        String telephone = request.getParameter("telephone");
        String summary = request.getParameter("summary");
        Bank bank = new Bank();
        ResultData resultData = new ResultData();
        bank.setName(name);
        bank.setBusinessNo(businessNo);
        bank.setCityArea(cityArea);
        bank.setAddress(address);
        bank.setTelephone(telephone);
        bank.setSummary(summary);

        int i = 0;
        i = bankSer.noQueryBank(bank);
        int num = 0;
        if (i == 0){
            num = bankSer.addBank(bank);
        }else {
            try {
                response.sendRedirect("/addBank.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        if (num>0){
            try {
                response.sendRedirect("/bank?action=queryBank");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
//        return resultData;
    }
}
